Segmentation fault while switching QCompleter for QLineEdit [on hold]

Posted by san on Programmers See other posts from Programmers or by san
Published on 2013-11-03T18:18:22Z Indexed on 2013/11/03 22:15 UTC
Read the original article Hit count: 323

Filed under:
|
|

I have a QLineEdit that uses autocompletion one which on focusIn event in which it shows paths from XML List(here I have used hardcoded list) but if user doesn't find the path from that list popped by QCompleter than I want user to be able to browse to path typing '/' in QLineEdit , I am not able to select the paths say /Users etc and on trying to type Segmentation fault occurs.

from PyQt4.Qt import Qt, QObject,QLineEdit
from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOT
from PyQt4 import QtGui, QtCore

import sys

class DirLineEdit(QLineEdit, QtCore.QObject):
    """docstring for DirLineEdit"""


    def __init__(self):
        super(DirLineEdit, self).__init__()
        self.defaultList = ['~/Development/python/searchMethod', '~/Development/Nuke_python', '~/Development/python/openexr', '~/Development/python/cpp2python']
        self.textChanged.connect(self.__dirCompleter)

    def focusInEvent(self, event):
        if len(self.text()) == 0:
            self._pathsList()
        QtGui.QLineEdit.focusInEvent(self, event)
        self.completer().complete()

    def __dirCompleter(self):
        if len(self.text()) == 0:
            model = MyListModel(self.defaultList, self)
            completer = QtGui.QCompleter(model, self)
            completer.setModel(model)
        else:
            dirModel = QtGui.QFileSystemModel() 
            dirModel.setRootPath(QtCore.QDir.currentPath()) 
            dirModel.setFilter(QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files) 
            dirModel.setNameFilterDisables(0) 
            completer = QtGui.QCompleter(dirModel, self)
            completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) 
            completer.setModel(dirModel)
        self.setCompleter(completer)

    def _pathsList(self):

        completerList = QtCore.QStringList()
        for i in self.defaultList:
            completerList.append(QtCore.QString(i))
        lineEditCompleter = QtGui.QCompleter(completerList)
        lineEditCompleter.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
        self.setCompleter(lineEditCompleter)

class MyListModel(QtCore.QAbstractListModel):
    def __init__(self, datain, parent=None, *args):
        """ datain: a list where each item is a row
        """
        QtCore.QAbstractTableModel.__init__(self, parent, *args)
        self.listdata = datain

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.listdata)

    def data(self, index, role):
        if index.isValid() and role == QtCore.Qt.DisplayRole:
            return QtCore.QVariant(self.listdata[index.row()])
        else:
            return QtCore.QVariant()


app = QtGui.QApplication(sys.argv)
smObj = DirLineEdit()
smObj.show()
app.exec_()

Please help fix this or suggest better way of implementation?

© Programmers or respective owner

Related posts about python

Related posts about auto-completion